/*
 * Author:  Ben Johnson
 * Company:  1186 Entertainment
 * Date Modified:  3/14/07
 * Filename:  EditProfile.java
 */

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class EditProfile extends JFrame implements ActionListener
{

	public ClientApplet CA;

	//declare components
	JLabel lblUserName;
	JLabel lblUserPwd;
	JLabel lblCnfUserPwd;
	JLabel lblEmail;
	JLabel lblGender;
	JLabel lblBirth;

	JTextField txtUserName;
	JPasswordField txtUsrPwd;
	JPasswordField txtCnfUsrPwd;
	JTextField txtEmail;
	JComboBox lstGender;
	JTextField txtGender;
	JTextField txtBirth;

	JButton btnSave;
	JButton btnCancel;


	String DELIM = "&";

	String username;
	String usrPwd;
	String cnfPwd;
	String email;
	String gender;
	String birth;

	public EditProfile(ClientApplet CA1, String[] data)
	{
		String oldUserName = data[1].trim();
		String oldPassword = data[2].trim();
		String oldEmail = data[3].trim();
		String oldGender = data[4].trim();
		String oldBirth = data[5].trim();

		CA = CA1;

		//apply the layout
		int rows = 7;
		int cols = 2;
		JPanel panel = new JPanel(new GridLayout(rows, cols));

		int top, left, bottom, right;
		top = left = bottom = right = 20;
		panel.setBorder(BorderFactory.createEmptyBorder (top,left,bottom,right));

		//initialize all the labels and fields

		lblUserName = new JLabel("Username: ");
		panel.add(lblUserName);
		txtUserName = new JTextField(oldUserName);
		txtUserName.setEditable(false);
		panel.add(txtUserName);

		lblUserPwd = new JLabel("Enter Password ");
		panel.add(lblUserPwd);
		txtUsrPwd = new JPasswordField(15);
		txtUsrPwd.setText(oldPassword);
		panel.add(txtUsrPwd);

		lblCnfUserPwd=new JLabel("Confirm Password ");
		panel.add(lblCnfUserPwd);
		txtCnfUsrPwd = new JPasswordField(15);
		txtCnfUsrPwd.setText(oldPassword);
		panel.add(txtCnfUsrPwd);

		lblEmail = new JLabel("E-Mail ");
		panel.add(lblEmail);
		txtEmail = new JTextField(oldEmail);
		panel.add(txtEmail);

		lblGender = new JLabel("Gender");
		panel.add(lblGender);
		//String[]  genderlist = {"male", "female"};
		//JComboBox lstGender = new JComboBox(genderlist);
		lstGender = new JComboBox();
		lstGender.addItem("male");
		lstGender.addItem("female");
		if(oldGender.equals("male"))
		{
			lstGender.setSelectedIndex(0);
			System.out.println("lstGender.setSelectedIndex = "+lstGender.getSelectedIndex());
		}
		else
		{
			lstGender.setSelectedIndex(1);
			System.out.println("lstGender.setSelectedIndex = "+lstGender.getSelectedIndex());
		}
		panel.add(lstGender);
		lstGender.addActionListener(this);
		//txtGender = new JTextField(oldGender);
		//txtGender.setEditable(false);
		//panel.add(txtGender);
		gender = oldGender;

		lblBirth = new JLabel("Birthdate (YYYY-MM-DD)");
		panel.add(lblBirth);
		txtBirth = new JTextField(oldBirth);
		panel.add(txtBirth);

		btnSave=new JButton("Update");
		btnSave.addActionListener(this); //add listener to the Submit button
		panel.add(btnSave);
		btnCancel=new JButton("Cancel");
		btnCancel.addActionListener(this); //add listener to the Cancel button
		panel.add(btnCancel);

		add(panel);
		panel.setVisible(true);
		setSize(500,300);
		setVisible(true);
	}//end constructor

	public void actionPerformed(ActionEvent e1)
	{
		Object src = e1.getSource();

		if(src == btnCancel)
		{
			this.dispose();
		}
		else if(src == btnSave)
		{
			if(verify())
			{
				try
				{
					CA.send("17"+ DELIM + username + DELIM + new String(usrPwd) + DELIM + email + DELIM + gender + DELIM + birth);
				}
				catch(Exception e)
				{
					System.out.println("Exception "+e);
				}
				this.dispose();
			}
		}
		else if(src == lstGender)
		{
			if(lstGender.getSelectedIndex() == 0) gender = "male";
			if(lstGender.getSelectedIndex() == 1) gender = "female";
			System.out.println("Gender was changed to "+ gender);
		}

	}//end of actionPerformed()



	public boolean verify() //test the validity of the user information
	{
		boolean valid = true;
		int intAge=0;
		try
		{
			username = txtUserName.getText().trim();

			char[] chrusrPwd = txtUsrPwd.getPassword();
			usrPwd = new String(chrusrPwd).trim();

			char[] chrcnfPwd = txtCnfUsrPwd.getPassword();
			cnfPwd = new String(chrcnfPwd).trim();

			email = txtEmail.getText().trim();

			if(lstGender == null) System.out.println("lstGender is null");
			System.out.println("lstGender.selectedIndex = "+lstGender.getSelectedIndex());
			if(lstGender.getSelectedIndex() == 0){gender = "male";}
			else {gender = "female";}
			//gender = txtGender.getText();

			birth = txtBirth.getText().trim();

			// Check form completion
			if( username == null || usrPwd == null || cnfPwd == null || email == null ||  gender == null ||  birth == null)
			{
				JOptionPane.showMessageDialog(this,"Incomplete form.", "Invalid Form", JOptionPane.ERROR_MESSAGE);
				return false;
			}

			// Check passwords
			if(!usrPwd.equals(cnfPwd))
			{
				JOptionPane.showMessageDialog(this,"Passwords are not the same", "Invalid Form", JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if(usrPwd.length() < 4)
			{
				JOptionPane.showMessageDialog(this,"Password must be at least 4 characters.", "Invalid Form", JOptionPane.ERROR_MESSAGE);
				return false;
			}

			if(gender.length() > 6)
			{
				JOptionPane.showMessageDialog(this,"Gender Field must be less than 7 characters.", "Invalid Form", JOptionPane.ERROR_MESSAGE);
				return false;
			}

			// Check birthday
			String birthParts[] = birth.trim().split("-");
			if( birthParts != null )
			{
				int intYear = (int)Integer.parseInt(birthParts[0]);
				int intMonth = (int)Integer.parseInt(birthParts[1]);
				int intDay = (int)Integer.parseInt(birthParts[2]);
				if(intYear < 1900 || intYear > 2007)
				{
					JOptionPane.showMessageDialog(this,"Invalid Year", "Invalid Form", JOptionPane.ERROR_MESSAGE);
					return false;
				}
				if(intMonth < 1 || intMonth > 12)
				{
					JOptionPane.showMessageDialog(this,"Invalid Month", "Invalid Form", JOptionPane.ERROR_MESSAGE);
					return false;
				}
				if(intDay < 0 || intDay > 31)
				{
					JOptionPane.showMessageDialog(this,"Invalid Day", "Invalid Form", JOptionPane.ERROR_MESSAGE);
					return false;
				}
			}
			else
			{
				JOptionPane.showMessageDialog(this,"Invalid birthday.  Use YYYY-MM-DD format.", "Invalid Form", JOptionPane.ERROR_MESSAGE);
				return false;
			}

		}//end of try
		catch(Exception e)
		{
			System.out.println("error in verify:");
			e.printStackTrace();
			JOptionPane.showMessageDialog(this,"Error on form.", "Message", JOptionPane.ERROR_MESSAGE);
			return false;
		}//end of catch
		return true;
	}//end of verify()






}//end of class
